package org.gitman.agent.platform;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import org.gitman.agent.gui.MainFrame;

public class PlatformManager {

	private MainFrame mainFrame;
	private Simulation simulation;
	private GraphEditor graphEditor;
	private List<Agent> allAgents = null;

	public PlatformManager(MainFrame mainFrame){
		this.mainFrame = mainFrame;
		simulation = new Simulation(this);
		graphEditor = new GraphEditor();
	}
	
	public Simulation getSimulation() {
		return simulation;
	}

	public GraphEditor getGraphEditor(){
		return graphEditor;
	}

	/**
	 * This method returns all agents defined in file. They are implementation of abstract Agent
	 * They should be used only to create new instatnces of this agents.
	 * @return
	 */
	public List<Agent> getAllAgents() {
		if(allAgents == null){
			allAgents = new LinkedList<Agent>();
			String confFile = System.getProperty("agents");
			Properties properties = new Properties();
			ClassLoader loader = PlatformManager.class.getClassLoader();
			try {
				properties.load(new FileInputStream(confFile));
				for(Object o : properties.values()){
					Agent agent = (Agent) loader.loadClass(o.toString()).newInstance();
					allAgents.add(agent);
				}
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		return allAgents;
	}

	public MainFrame getMainFrame(){
		return mainFrame;
	}


	public void newSimulation() {
		simulation = new Simulation(this);
		mainFrame.getSimulationPanel().updateAgentsTab();		
	}
}
